<?php
/**
 * 學習描述文字
 * @author cik
 *
 */
class Score_DescriptionController extends Zend_Controller_Action
{
    private $_teacherSn;

    public function init()
    {
        // 教師 sn
        $this->_teacherSn = Teacher_Model_Teacher::getSnById();
        /* Initialize action controller here */
    }

    /**
     * 儲存評語
     * @return unknown_type
     */
    private  function _save()
    {
        $request = $this->getRequest();
        $sn = $request->getPost('sn',0);
        // 建立階段成績 object
        $scoreStage = new Score_Model_Description($sn);
        // 檢查是否為自己的配課
        if ($this->_teacherSn != $scoreStage->getTeacherCourse()->teacher_sn) {
            $this->_redirect('score/stage/error');
        }
        // 課程代號
        $subjectSn = $scoreStage->getTeacherCourse()->subject_sn;
        // 評語陣列
        $descriptionArr = $request->getPost('description');
        // 努力程度
        $effortsArr = $request->getPost('efforts');

        foreach($effortsArr as $studentSn=>$val) {
            $description = strip_tags($descriptionArr[$studentSn]);
            $effort = (int)$val;
            $row = Doctrine_Query::create()
            ->select('sn')
            ->from('Student_Model_StudentScoreSemester')
            ->where('student_sn=?',$studentSn)
            ->andWhere('subject_sn=?',$subjectSn)
            ->fetchOne();
            if ($row) { // 修改
                $q = Doctrine_Query::create()
                ->update('Student_Model_StudentScoreSemester')
                ->set('description','?',$description)
                ->set('efforts','?',$effort)
                ->set('teacher_sn','?',$this->_teacherSn)
                ->where('sn=?',$row->sn);
                $q->execute();
            }
            else { // 新增
                $studentScoreSemester = new Student_Model_StudentScoreSemester();
                $studentScoreSemester->subject_sn = $subjectSn;
                $studentScoreSemester->student_sn = $studentSn;
                $studentScoreSemester->description = $description;
                $studentScoreSemester->efforts = $effort;
                $studentScoreSemester->teacher_sn = $this->_teacherSn;
                $studentScoreSemester->save();
            }
        }
        //$this->_redirect('score/description/index');
    }

    public function indexAction()
    {
        if ($this->getRequest()->getParam('editMode'))
        $this->_save();

        $year = $this->view->Semester['year'];
        $semester = $this->view->Semester['semester'];
        // 班級陣列
        $semesterClass = new Semester_Model_SemesterClass;
        $this->view->class_arr = $semesterClass->getClassNames($year,$semester);

        // 取任教班級科目
        // TODO 分組班科目加入選單中
        $model = new Semester_Model_SemesterTeacherCourse();
        $teacherCourse = $model->getTeacherCourse($year,$semester,$this->_teacherSn,Semester_Model_SemesterTeacherCourse::SCORED);

        if (count($teacherCourse) ==0)
        $this->_redirect('score/stage/yet-assign');

        $this->view->teacherCourse = $teacherCourse;
        $sn = $this->getRequest()->getParam('sn');
        if (!$sn)
        $sn = (int) current(array_keys($teacherCourse));

        // 選擇配課科目流水號
        $this->view->sn = $sn ;

        // 建立階段成績 object
        $scoreStage = new Score_Model_Description($sn);

        // 檢查是否為自己的配課
        if ($this->_teacherSn != $scoreStage->getTeacherCourse()->teacher_sn) {
            $this->_redirect('score/stage/error');
        }

        // 本學期學生名冊

        $this->view->students = $scoreStage->getStudents();

        // 努力程度
        $systemText = new Default_Model_SystemText();
        $this->view->effortsLevel = $systemText->getKindArray('efforts_level');

        $this->view->scores = $scoreStage->getScores();
        // 評語類別
        $this->view->commentKind = $scoreStage->getCommentKind();
        // 評語等級
        $this->view->commentLevel = $scoreStage->getCommentLevel();
    }

    /**
     * 回傳評語
     * @return json
     */
    public function getCommentAction()
    {
        $kindSn = (int)$this->getRequest()->getParam('kind_sn',0);
        $levelSn = (int)$this->getRequest()->getParam('level_sn',0);

        $q = Doctrine_Query::create()
        ->select('sn, text')
        ->from('Score_Model_Comment')
        ->where('kind_sn=?', $kindSn)
        ->andWhere('level_sn=?', $levelSn)
        ->orderBy('teacher_sn DESC, sn');
        $res = $q->fetchArray();
        //        echo $q->getSqlQuery();
        $arr = array();
        foreach($res as $row) {
            if ($row['text'] == '')
            continue;
            //            $text = mb_substr($row['text'],0,10,'UTF-8');
            //            if (mb_strlen($row['text'],'UTF-8') > 10)
            //            $text .= '...';
            //            $arr[$row['sn']] = $row['sn'].' '.$text ;
            $arr[$row['sn']] = $row['sn'].' '.$row['text'];

        }

        $this->_helper->json($arr);
    }

}

